from myelin.rdf.Ns import RDFS_SEEALSO
from myelin.rdf.bindery.Model import OBJECT_TYPE_RESOURCE
from nodetypes.ntypes.Document.view.RDF import Transform, Document


class Document(Document):
    
    def getRelatedStatements(self, context, consumer, driver, locale):
        return self.getContainerStatements(
            context,
            super(Document,self).getRelatedStatements(
                context, consumer, driver, locale
            ),
            driver,
            locale
        )
    
    def getContainerStatements(self, context, consumer, driver, locale):
        #root = context.defaults.document_root
        gas = self.getAgentStatements
        
        # order nodes into their groupings
        for n in self.filterContainedNodes(
            context,
            consumer,
            [],
            driver,
            locale
        ):
            nrs = n.rdf_toStatements
                        
            consumer(nrs('rdfType'))    
            consumer(nrs('mTag'))
            consumer(nrs('dcTitle'))
            #consumer(nrs('dcDescription'))
            consumer(nrs('dcLanguage'))
            
            #consumer(nrs('dctermsCreated'))
            #consumer(nrs('dcCreator'))
            #for m in n.dcCreator:
            #    gas(context, consumer, m, locale) #, root)
            
            #consumer(nrs('dctermsModified'))
            #consumer(nrs('mLast_Modifier'))
            #for m in n.mLast_Modifier:
            #    gas(context, consumer, m, locale) #, root)
        
        return consumer
    
    def getContainedNodeInitializer(self, context):
        def init(n):
            n.rdf_getDomain()
            return n
        return init
    
    def filterContainedNodes(self, context, consumer, nodes, driver, locale, nfilter=lambda n: True):
        # filter only nodes that have matching acl
        action = context.action
        acls = context.acls.checkNodeAcls
        model = context.model.rdf_clone(driver)
        
        #model = model.rdf_clone(self.contentDriver)
        mid = model.rdf_value
        
        # MAX DISPLAY LIMIT
        # if len(nodes) == max_display_limit:
        #   return
        # Should this be a user preference???
        key = self.containsKey
        href = context.urlFormatter.newUrlMapper(True)
        init = self.getContainedNodeInitializer(context)
        # filter direct children nodes
        n_append = nodes.append        
        for np, n in model.rdf_toRelations(key):
            #n = n.rdf_clone(driver)
            n = init(n)
            if nfilter(n) and \
                acls(n.rdf_domain, action):
                n_append(n)
                
                consumer((
                    n.rdf_asStatement(
                        np,
                        mid
                    ),
                ))
                
        for tp,t in model.rdf_toRelations('mTranslations'):
            #t = t.rdf_clone(driver)
            tid = t.rdf_value
            thit = 0
            for cp,c in t.rdf_toRelations(key):
                cid = c.rdf_value
                chit = 0
                for np, n in c.rdf_toRelations('mTranslation'):
                    if n.rdf_value != mid and \
                        locale in n.dcLanguage and \
                        nfilter(n) and \
                        acls(n.rdf_domain, action):                                    
                        n_append(n)
                        
                        consumer((
                            n.rdf_asStatement(
                                np,
                                cid
                            ),
                        ))
                        nrs = n.rdf_toStatements
                        
                        consumer(nrs('rdfType')) 
                        consumer(nrs('dcTitle'))
                        consumer(nrs('dcLanguage'))
                        
                        o = href(n.rdf_domain)
                        if o:
                            consumer((
                                (
                                    n.rdf_value,
                                    RDFS_SEEALSO,
                                    o,
                                    '',
                                    n.rdf_domain,
                                    OBJECT_TYPE_RESOURCE,
                                ),
                            ))
                        
                        chit = 1
                if chit:
                    consumer((
                        c.rdf_asStatement(
                            cp,
                            tid
                        ),
                    ))
                    nrs = c.rdf_toStatements
                        
                    consumer(nrs('rdfType')) 
                    consumer(nrs('dcTitle'))
                    consumer(nrs('dcLanguage'))
                    
                    o = href(c.rdf_domain)
                    if o:
                        consumer((
                            (
                                c.rdf_value,
                                RDFS_SEEALSO,
                                o,
                                '',
                                c.rdf_domain,
                                OBJECT_TYPE_RESOURCE,
                            ),
                        ))
                    
                    thit = 1
            if thit:
                consumer((
                    t.rdf_asStatement(
                        tp,
                        mid
                    ),
                ))
                
                nrs = t.rdf_toStatements
                        
                consumer(nrs('rdfType')) 
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                
                o = href(t.rdf_domain)
                if o:
                    consumer((
                        (
                            t.rdf_value,
                            RDFS_SEEALSO,
                            o,
                            '',
                            t.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
                
        return nodes
    

class Transform(Transform):
    
    newDocument = Document